package com.qinglei.recoup.system.task;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qinglei.recoup.common.utils.DateUtil;
import com.qinglei.recoup.system.dao.BedHealthBreathDailyDataMapper;
import com.qinglei.recoup.system.dao.BedHealthDailyStatisticsMapper;
import com.qinglei.recoup.system.dao.TBedHealthDailyStatisticsDataMapper;
import com.qinglei.recoup.system.dao.TableFeaturesMapper;
import com.qinglei.recoup.system.domain.*;
import com.qinglei.recoup.system.service.BedService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Date;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;

/**
 * 院长端数据清晰
 */
@Slf4j
@Component
public class DeanDataTask {

    @Autowired
    private TBedHealthDailyStatisticsDataMapper tBedHealthDailyStatisticsDataMapper;
    @Autowired
    private BedHealthDailyStatisticsMapper bedHealthDailyStatisticsMapper;
    @Autowired
    private TableFeaturesMapper tableFeaturesMapper;
    @Autowired
    private BedHealthBreathDailyDataMapper bedHealthBreathDailyDataMapper;

    @Autowired
    private BedService bedService;

    public void pushDeanData() {
        log.info("pushDeanData -护工数据统计任务- run");
        LocalDate yesterday= LocalDate.now().minusDays(1);
        tBedHealthDailyStatisticsDataMapper.deleteByDate(yesterday);
        List<BedHealthDailyStatistics> bedHealthDailyStatistics = bedHealthDailyStatisticsMapper.selectListByDate(yesterday);
        TBedHealthDailyStatisticsData td;
        for (BedHealthDailyStatistics b : bedHealthDailyStatistics) {
            td = new TBedHealthDailyStatisticsData();
            BeanUtils.copyProperties(b, td);
            td.setStatisticsDate(b.getStatisticsDate());
            td.setScore(Double.valueOf(b.getScore()));
            td.setCreateTime(b.getCreateTime());
            BedRadarBO bedWithSn = bedService.getBedWithSn(b.getBedId());
            if (bedWithSn != null) {
                TableFeatures tableFeatures = tableFeaturesMapper.selectDate(bedWithSn.getSnCode(), b.getStatisticsDate().toString());
                if (tableFeatures != null) {
                    td.setVitalityIndex(tableFeatures.getVitalityIndex());
                    td.setMoraleIndex(tableFeatures.getMoraleIndex());
                    td.setFallIndex(tableFeatures.getFallIndex());
                }
                BedHealthBreathDailyData bl = bedHealthBreathDailyDataMapper.selectByDate(b.getBedId(), b.getStatisticsDate());
                if (bl != null) {
                    td.setAhi(bl.getAhi());
                    td.setHi(bl.getHi());
                    td.setAi(bl.getAi());
                }

                tBedHealthDailyStatisticsDataMapper.insertData(td);

            }
        }
    }

    public  void TestPushDeanData(){
      List<String> list=  DateUtil.getBetweenDate("2021-08-20","2021-08-30");
        list.forEach(l->{
            //pushDeanData(l);
        });
    }

}
